<template>
  <!-- 查询栏 -->
  <div class="query">
    <el-tabs
      v-model="curStatus"
      class="demo-tabs"
      @tab-click="handleClick"
      style="background: #fff; padding: 0 20px"
    >
      <el-tab-pane label="待付款" name="0"></el-tab-pane>
      <el-tab-pane label="已取消" name="1"></el-tab-pane>
      <el-tab-pane label="待发货" name="2"></el-tab-pane>
      <el-tab-pane label="已发货" name="3"></el-tab-pane>
      <el-tab-pane label="已收货" name="4"></el-tab-pane>
      <el-tab-pane label="已完成" name="5"></el-tab-pane>
    </el-tabs>
    <QueryForm :form="form" v-model="queryData" @handleQuery="handleQuery" />
  </div>
  <!-- 表单栏 -->
  <div class="table">
    <BaseTable
      v-loading="tableLoading"
      :columns="columns"
      :data="dataList"
      :pager="pager"
      @handleCurrentChange="handleCurrentChange"
      @handleAction="handleAction"
    >
      <template v-slot:orderItem>
        <el-table-column prop="user_id" label="用户id" align="center" />
        <el-table-column label="商品信息" width="240" align="center">
          <template #default="scope">
            <template
              v-for="(orderItem, index) in scope.row.order_items"
              :key="index"
            >
              <div
                style="display: flex; flex-direction: row; align-items: center"
              >
                <el-avatar
                  shape="square"
                  size="large"
                  :src="`${orderItem.product_img}`"
                  style="flex-shrink: 0"
                />
                <div style="margin-left: 5px; text-align: left">
                  <div>
                    商品名称:<span
                      style="color: darkslateblue; font-weight: bold"
                      >{{ orderItem.product_name }}</span
                    >
                  </div>
                  <div>
                    商品数量:
                    <span class="num">{{ orderItem.num }}</span>
                  </div>
                  <div>
                    商品价格:<span class="price">￥{{ orderItem.price }}</span>
                  </div>
                </div>
              </div>
            </template>
          </template></el-table-column
        >
      </template>
    </BaseTable>
    <!-- 弹窗 -->
    <el-dialog title="查看详情" v-model="showModal">
      <el-card class="box-card mb-10">
        <template #header>
          <div class="card-header">
            <span>订单信息</span>
            <span
              >状态：
              <span style="color: darkred; font-weight: bold">{{
                statusEnum[orderDetail.status]
              }}</span></span
            >
          </div>
        </template>
        <div class="text item mb-10">
          订单号：<span style="font-weight: bold">{{
            orderDetail.order_code
          }}</span>
        </div>
        <div class="text item mb-10">
          下单时间：{{ orderDetail.created_time }}
        </div>
        <div class="text item mb-10">
          数量：<span class="num">{{ orderDetail.num }}</span>
        </div>
        <div class="text item mb-10">
          价格：<span class="price">{{ orderDetail.total_price }}</span>
        </div>
      </el-card>
      <el-card class="box-card mb-10">
        <template #header>
          <div class="card-header">
            <span>商品信息</span>
          </div>
        </template>

        <template
          v-for="(orderItem, index) in orderDetail.order_items"
          :key="index"
        >
          <div
            style="
              display: flex;
              flex-direction: row;
              align-items: center;
              margin-bottom: 10px;
            "
          >
            <el-avatar
              shape="square"
              size="large"
              :src="`${orderItem.product_img}`"
            />
            <div style="margin-left: 5px">
              <div>
                商品名称:<span
                  style="color: darkslateblue; font-weight: bold"
                  >{{ orderItem.product_name }}</span
                >
              </div>
              <div>
                商品数量:
                <span style="color: darkgreen; font-weight: bold">{{
                  orderItem.num
                }}</span>
              </div>
              <div>
                商品价格:<span style="color: crimson; font-weight: bold"
                  >￥{{ orderItem.price }}</span
                >
              </div>
            </div>
          </div>
        </template>
      </el-card>
      <el-card class="box-card mb-10">
        <template #header>
          <div class="card-header mb-10">
            <span>收货地址</span>
          </div>
        </template>
        <div class="text item mb-10">暂无开放</div>
      </el-card>
      <el-card class="box-card mb-10">
        <template #header>
          <div class="card-header">
            <span>物流信息</span>
          </div>
        </template>
        <div class="text item mb-10">暂无开放</div>
      </el-card>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup>
import { reactive, ref, onMounted } from "vue";
import { getOrderList, orderUpdateStatus } from "@/api/order";
import { ElMessage } from "element-plus";
import type { TabsPaneContext } from "element-plus";
import { ResultVo } from "@/types/common";

// 弹窗变量
const showModal = ref(false);

// 参数
const pager = reactive({
  page: 1,
  limit: 10,
  total: 0,
});

// 生成查询form结构数据
const form = [
  {
    type: "input",
    label: "订单编号",
    model: "order_code",
    placeholder: "请输入订单编号",
  },
  {
    type: "input",
    label: "用户ID",
    model: "user_id",
    placeholder: "请输入用户id",
  },
  // {
  //   type: "select",
  //   label: "订单状态",
  //   model: "status",
  //   placeholder: "请选择状态",
  //   options: [
  //     { label: "待付款", value: 0 },
  //     { label: "取消", value: 1 },
  //     { label: "待发货", value: 2 },
  //     { label: "已发货", value: 3 },
  //     { label: "已收货", value: 4 },
  //     { label: "已完成", value: 5 },
  //   ],
  // },
];

const statusEnum: any = {
  0: "待付款",
  1: "取消",
  2: "待发货",
  3: "已发货",
  4: "已收货",
  5: "已完成",
};

// 表单loading
const tableLoading = ref(false);

// 初始化用户表单对象
const queryData = ref(<any>{ status: 0 });
const curStatus = ref(0);

// 定义动态表格的格式
const columns = reactive([
  {
    label: "订单号",
    prop: "order_code",
    slotName: "orderItem",
  },
  {
    label: "订单数量",
    prop: "num",
  },
  {
    label: "订单价格",
    prop: "total_price",
  },
  {
    type: "tag",
    label: "状态",
    prop: "status",
    formatter: (row: any, column: any, value: number) => {
      return statusEnum[value];
    },
    dict: { 0: "danger", 1: "success" },
  },
  {
    label: "下单时间",
    prop: "created_time",
    width: 180,
  },
  {
    label: "更新时间",
    prop: "updated_time",
    width: 180,
  },
  {
    type: "action",
    label: "操作",
    width: 140,
    list: [
      { text: "取消", cond: { prop: "status", value: [0] },confirm: true  },
      { text: "发货", cond: { prop: "status", value: [2] },confirm: true  },
      { text: "完成", cond: { prop: "status", value: [4] },confirm: true  },
      { text: "查看详情" },
    ],
  },
]);

// 渲染数据
const dataList = ref(<any>[]);

// 订单详情
const orderDetail = ref(<any>{});

onMounted(() => {
  handleQuery();
});

const handleClick = (tab: TabsPaneContext, event: Event) => {
  // console.log(tab, event);
  console.log(queryData.value.status);
  handleQuery();
};

// 查询
const handleQuery = async () => {
  tableLoading.value = true;
  let data = (
    await getOrderList({
      ...pager,
      ...queryData.value,
      status: curStatus.value,
    })
  ).data;
  dataList.value = data.rows;
  pager.total = data.count;
  tableLoading.value = false;
};

// 分页事件处理
const handleCurrentChange = (current: number) => {
  pager.page = current;
  handleQuery();
};

// 操作
const handleAction = async ({ text, row }: any) => {
  if (text === "查看详情") {
    showModal.value = true;
    orderDetail.value = { ...row };
    console.log("orderDetail", orderDetail.value);
  }
  if (text === "取消" || text === "发货" || text === "完成") {
    let status;
    if (text === "取消") status = 1;
    if (text === "发货") status = 3;
    if (text === "完成") status = 5;
    const res: any = await orderUpdateStatus({
      id: row.id,
      status,
    });
    if (res && res.code == 200) {
      ElMessage({ message: "操作成功", type: "success" });
      handleQuery();
    }
  }
};
</script>

<style lang="scss" scoped></style>
